/* ************************************************************************************************

	Copyright (c) 2005-2006, IGR
	All Rights Reserved.
	Licensed under the Apache License version 2.0 or above.
	For more information on licensing, see:
        http://www.apache.org/licenses/


 ************************************************************************************************
 Objet ABSTRACTCOMPONENT
 VERSION:0.85
 DATE:15/06/2006
 ************************************************************************************************
Init parameter in objPar (object of parameter):
Name		Name of the HTML element	
Type		type of widget	checkbox
top			Top position (in pixel)	
left		Left position (in pixel)	
width		Width (in pixel)
height		Height (in pixel)
position	position absolute or relative
visible		indicate if the object is visible or not
enable		indicate if the object is enable or not
draggable	indicate if the object is draggable or not
resizable	indicate if the object is resizable or not
autoResizableH	take the parent available height according to his top
autoResizableW	take the parent available width according to his left
*************************************************************************************************/
/* 
This object is an abstraction which inherits all the widgets

for each propertie you'll find a setter method
    
a external div is create but not add in the DOM

To insert the object in the DOM you must use the placeIn method
-if the parent is a rialto object then his add method is use
-if not just make an appendChild and try to find a rialto container in the upper tree

the parent property is always the HTML parentNode
the inParent property is the object parent (it could be an html or a javascript object)
the inContainer property is the first rialto container in ancestor of the object
    
The object has event method where the code must be redefine
    -onClick
    -onDbleClick
    -onMouseOver
    -onMouseOut
    -onMouseDown
    -onMouseUp
    -onBlur
    -onfocus
    
The object can have resize and drag and drop behavior initialize with the following function
    -initDD
    -initRZ

The object has also methods to retrieve HTML elements
    -getHtmlExt:return the more external DIV
    -getHtmlImp:return the printable element
    -getHtmlDD:return the draggable element
    -getHtmlRZ:retrun the resizable element
*/

// *************************************
// CONSTRUCTOR
// *************************************

rialto.widget.AbstractComponent=function(objPar){
//=======================================================
	this.bRiaComp = true;
    
    // meta
    this.type="rialto.widget.AbstractComponent";          
    this.name= "kindOf"+this.type;
	
	//DEFAULT VALUE
	
	// position
    this.position	= 'absolute'; 
    this.top 		=0;
    this.left 		= 0;
    this.width 		=  '100%';
    this.height 	=  '100%';                        

    this.visible	= true;        
    
    // behaviors
    this.enable=true;                
    this.draggable=false;            
    this.resizable=true;             
    this.autoResizableH=false;       
    this.autoResizableW=false;       
    this.bWithoutPlaceIn = false;    // if possible placeIn during the creating
    
    // other parameters management
    if (objPar != null){
        if (objPar.bWithoutPlaceIn) {
            this.bWithoutPlaceIn = true;        
        }
        if (objPar.name){
            this.name = objPar.name;        
        }
        if (objPar.type){
            this.type = objPar.type;        
        }
        if (rialto.lang.isNumber(objPar.top)){
            this.top = objPar.top;
        }
        if (rialto.lang.isNumber(objPar.left)){
            this.left = objPar.left;        
        }
        if (rialto.lang.isNumber(objPar.width)){
            this.width = objPar.width;        
        } 
        if (rialto.lang.isNumber(objPar.height)){
            this.height = objPar.height;        
        }
        if (rialto.lang.isStringIn(objPar.position,["static","absolute","relative"])){
            this.position = objPar.position;        
        }        
        if (rialto.lang.isBoolean(objPar.enable)){
            this.enable = objPar.enable;        
        }        
        if (rialto.lang.isBoolean(objPar.draggable)){
            this.draggable = objPar.draggable;        
        }
        if (rialto.lang.isBoolean(objPar.resizable)){
            this.resizable = objPar.resizable;        
        } 
        if (rialto.lang.isBoolean(objPar.autoResizableH)){
            this.autoResizableH = objPar.autoResizableH;        
        } 
        if (rialto.lang.isBoolean(objPar.autoResizableW)){
            this.autoResizableW = objPar.autoResizableW;        
        }             
        
        //ID
    	this.id= name+"_"+this.type+"_"+(rialto.widget.AbstractComponent.prototype.nbreInstance++);
    
    
    	//GLOBAL COLLECTION
    	rialto.session.reccord(this,this.id);
	
	    //EXTERNAL DIV
	    this.divExt=document.createElement("DIV");
	    this.divExt.id = this.id + '_divExt';             	    
    }                       
}

rialto.widget.AbstractComponent.prototype.nbreInstance=0;

/**********************************************************/
/************************ SETTER **************************/
/**********************************************************/

rialto.widget.AbstractComponent.prototype.moveTo = function(top,left){  
//=======================================================    
	this.setTop(top);
	this.setLeft(left);
}

//Change left
rialto.widget.AbstractComponent.prototype.setLeft=function(left){  
//=======================================================   
    var oHtml=this.getHtmlExt();
	if ((left == null) &&  (this.left != null)){
	    oHtml.style.left= this.left;  
    }
	else{
		if (typeof left != 'undefined') {
		    this.left=left;
		    oHtml.style.left=this.left;  
		}
	}
}

//Change  top
rialto.widget.AbstractComponent.prototype.setTop=function(top){   
//=======================================================
    var oHtml=this.getHtmlExt();
	if ((top == null) &&  (this.top != null)){
	    oHtml.style.top = this.top;  
    }
	else{
		if (typeof top != 'undefined') {
		    this.top=top;
		    oHtml.style.top=this.top;  
		}
	}	
}

//Change  width
rialto.widget.AbstractComponent.prototype.setWidth=function(width){
//=======================================================    
    var oHtml=this.getHtmlExt();
	if ((width == null) &&  (this.width != null)){
	    oHtml.style.width = this.width;  
    }
	else{
		if (typeof width != 'undefined') {
		    this.width=width;
		    oHtml.style.width=this.width;  
		}
	}
}

//Change  height
rialto.widget.AbstractComponent.prototype.setHeight=function(height){
//=======================================================   
   var oHtml=this.getHtmlExt();
	if ((height == null) &&  (this.height != null)){
	    oHtml.style.height= this.height;  
    }
	else{
		if (typeof height != 'undefined') {
		    this.height=height;
		    oHtml.style.height=this.height;  
		}
	}
}

//Change visible state
rialto.widget.AbstractComponent.prototype.setVisible=function(visible){
//=======================================================        
    var oHtml=this.getHtmlExt();
    if(visible){    
        oHtml.style.display='block';
    }
    else{
        oHtml.style.display='none';
    }
    this.visible=visible;
}

//get visible state
rialto.widget.AbstractComponent.prototype.getVisible=function(){
//=======================================================        
    return this.visible;
}

//Change enable state
rialto.widget.AbstractComponent.prototype.setEnable=function(enable){
//=======================================================           
}

//get enable state
rialto.widget.AbstractComponent.prototype.getEnable=function(){
//=======================================================      
		return this.enable;     
}

//Change le comportement DD
rialto.widget.AbstractComponent.prototype.setDraggable=function(draggable){
//=======================================================        
    traceExec('draggable:'+draggable,1);
    this.draggable		= draggable;    
    var objDD			= this.getHtmlDD();    
    objDD.dragAndDrop	= this.draggable;   
}

//Change  position
rialto.widget.AbstractComponent.prototype.setPosition=function(position){  
//=======================================================    
    this.position=position;    
    var oHtml=this.getHtmlExt();
    oHtml.style.position=this.position;   
}

//CHANGE A STYLE ATTRIBUTE
rialto.widget.AbstractComponent.prototype.setStyle=function(obStyle) {       
//=======================================================
    //recupere l'elmt sup    
    var oHtml=this.getHtmlExt();
    for (prop in obStyle){
    	oHtml.style[prop]=obStyle[prop];
    }
}
rialto.widget.AbstractComponent.prototype.getStyle=function(obStyle) {       
//=======================================================
// if obStyle is an object with attribut of style
//   return obStyle with all attributes fixed 
//   as the style attributes of divExt
// if obStyle is a string 
//   return this style atribute of divExt

    var oHtml=this.getHtmlExt();

    try {         
		if (typeof (obStyle) == 'string') 
 		   	obStyle = getComputStyle(oHtml,obStyle); 
		else {
	
		    for (prop in obStyle){      
		        	eval("obStyle."+prop+" =   getComputStyle(oHtml,'"+ prop+"');"); 
		    }
	    }
    }
    catch (erreur) {}

    return obStyle;
}


/**********************************************************/
/******************* ADD/REMOVE/update  **********************/
/**********************************************************/
rialto.widget.AbstractComponent.prototype.replaceInOriginalParent =function() {
	//to refine ...
	//replace the GUI component after foreground plact
	if (!rialto.session.objects) rialto.session.objects = new Array;
	if (rialto.session.objects["uicInTop"]) {
		var elt = rialto.session.objects["uicInTop"].uic;
		//insuffisant : il faut conserver un fantome ds l arbre dom
		rialto.session.objects["uicInTop"].parent.appendChild(elt);
		elt.style.zIndex = rialto.session.objects["uicInTop"].zIndex;
		rialto.session.objects["uicInTop"] = null;
	}    	
}


rialto.widget.AbstractComponent.prototype.placeIn = function(parent) {
//====================================================
//Registration in the hierarchy of components instances
	if (!parent && !this.parent) return;   
    if (parent == this.inParent) return;

    if (!parent)
    	parent = this.parent;
    	
    //traceExec('placeIn',1);    
    //-- get the outer Html object   
    var oHtml=this.getHtmlExt();  

    //-- link rialto component instance and obj Html
    oHtml.oCiu = this;     //old naming
    oHtml.oRia = this;
  
    
  
    //-- call rialto object add method if there is one      
    if (parent.add){   
        parent.add(this,true); 
    }
    //-- use API DOM function in Html object else
    else{
        parent.appendChild(oHtml);         
        this.addInRialtoContainer(parent);
    }
  	
    this.afterPlaceIn();
    this.inParent = parent;

}
//Try to find a rialto container to reccord in it
//look for the first HTML element with oRia propertie
rialto.widget.AbstractComponent.prototype.addInRialtoContainer=function(objHTML){    
    if (!objHTML) return;
    //look if the HTML element has a rialto pointer
    if (objHTML.oRia){                
        //look if it is a container
        if(objHTML.oRia.isContainer){
            objHTML.oRia.record(this)
        }
        //reccord in his container
        else{
       		//objHTML.oRia.inContainer.record(this)
       		this.addInRialtoContainer(objHTML.parentNode)
        }        
    }
    //go upper
    else{
    	this.addInRialtoContainer(objHTML.parentNode)
    }    
}

rialto.widget.AbstractComponent.prototype.afterPlaceIn = function() {
	if(this.alreadyAfterPlaceIn){return;}
	var oHtml=this.getHtmlExt();  
	
	//-- save old parent container
    if (this.inParent) {
    	var oldParent = this.parent;
    }
    
	this.parent=oHtml.parentNode;    
	//-- if there's one, inform old container 
    if (oldParent && oldParent.removeContents)
    	oldParent.removeContents(this);
	//-- add events handler
    //this.addEvtHandler(oHtml);
   	//this.initRZ();      
    //-- compute measures, execute actions  ... etc
    //   according to the new component context 
    this.adaptToContext();       
    this.initDD();
    this.alreadyAfterPlaceIn=true;
}


rialto.widget.AbstractComponent.prototype.placeInTop = function () {
//====================================
//to refine : modify placeIn and use it
//place the GUI component in the foreground (document's son & higher zIndex)
this.replaceInOriginalParent();
if (!rialto.session.objects) rialto.session.objects = new Array;
var elt = this.getHtmlExt() ;
rialto.session.objects["uicInTop"] = { uic:this
							  ,parent: this.parent
							  ,zIndex: elt.style.zIndex};

elt.style.zIndex = 1000002;
window.document.body.appendChild(elt);
							  
}

//REMOVE FROM PARENT
//parameter bFromContainer:true if the remove is cause by the container remove
rialto.widget.AbstractComponent.prototype.remove = function(bFromContainer){          
//================================         

	//release specific references
	this.release();
	// remove Html elts    
    var oHtml=this.getHtmlExt();
    if(oHtml.parentNode){oHtml.parentNode.removeChild(oHtml);}

	// inform the container of remove (if the remove is not cause by the container remove)
	if(!bFromContainer){
		if (this.inContainer && this.inContainer.removeContents)
			this.inContainer.removeContents(this);
	}
		    
     //remove global reference  
     rialto.session.objects[this.id]=null;
     this.parent=null;
     this.inParent=null;
     oHtml.oCiu=null;
     oHtml.oRia=null;
     for (prop in this){this[prop]=null};
}

//rialto.widget.AbstractComponent.prototype.majElemts=function(){
rialto.widget.AbstractComponent.prototype.adaptToContext = function(){
//=====================================================
// update component according to the modification of context 
}

//rialto.widget.AbstractComponent.prototype.majElemts=function(){
rialto.widget.AbstractComponent.prototype.adaptAfterContainerChange = function(){
//=====================================================
// update component according to the modification of context 
}

rialto.widget.AbstractComponent.prototype.release = function(){
//=====================================================
// to release all references 
//according to bug RialTo and memory leak in Internet Explorer and FireFox mention by Toshiyuki KIMURA
//http://bug.improve.fr/view.php?id=246
}
/**********************************************************/
/**************** Drag & drop/ Resize *********************/
/**********************************************************/

//FONCTION D'INITIALISATION DU D&D
rialto.widget.AbstractComponent.prototype.initDD=function(){
//=======================================================
    //Ajout du D&D
    if (this.draggable) {
        //by default the outer div is the div  to move   
        var oHtml=this.getHtmlExt();            
        //the div which is affected by the behavior and capture DD the event  
        var objDD=this.getHtmlDD();        

/*
        objDD.oCiu=this;

		if (rialto.config.isTestVersion) {
	        objDD.DD = rialto.widgetBehavior.DragAndDrop;
	        objDD.DD({
	        			oHtmlToMove	: oHtml
			            ,modCursor	: {avantDD : true, auClic : true}
	        		});
        }
        else {
*/
			rialto.widgetBehavior.affect(this
										,'DragAndDrop'
										,{ oHtmlToMove 			  : oHtml
										  ,oHtmlEvtTarget		  : objDD
    							    	  ,isWithLimitsDisplayed  : false}
    							    	  );
//or:
/*
			rialto.widgetBehavior.affect(objDD
										,'DragAndDrop'
										,{ oHtmlToMove 			  : oHtml
    							    	  ,isWithLimitsDisplayed  : false}
    							    	  );
*/
//		}
    }
}

//FONCTION D'INITIALISATION DU RESIZE
rialto.widget.AbstractComponent.prototype.initRZ=function(){
//=======================================================

    //Ajout du RZ
    /*if (this.resizable) {
        //recupere l'elmt sup    
        var oHtml=this.getHtmlExt();    
        //recupere l'elmt RZ    
        var objRZ=this.getHtmlRZ();                
        objRZ.oCiu=this;
        objRZ.comportementReDim = ReSize;   
        objRZ.comportementReDim();
        objRZ.synchro=function(objSync){  
            //traceExec("objSync.width="+objSync.width,4);
            //traceExec("this.oCiu.divExt.style.width="+this.oCiu.divExt.style.width,4);
            this.oCiu.setWidth(parseInt(this.oCiu.divExt.style.width)+objSync.width);
            this.oCiu.setHeight(parseInt(this.oCiu.divExt.style.height)+objSync.height);    
        }
    }*/     
}


/**********************************************************/
/**** FUNCTION TO RETRIEVE HTML ELEMENTS			 ******/
/**********************************************************/

//EXTERNAL DIV
rialto.widget.AbstractComponent.prototype.getHtmlExt=function(){
//=======================================================    
    return this.divExt;
}

//PRINTABLE DIV
rialto.widget.AbstractComponent.prototype.getHtmlImp=function(){
//=======================================================    
    return this.divExt;
}

//DRAGGABLE DIV
rialto.widget.AbstractComponent.prototype.getHtmlDD=function(){
//=======================================================    
    return this.divExt;
}

//RESIZABLE DIV
rialto.widget.AbstractComponent.prototype.getHtmlRZ=function(){
//=======================================================    
    return this.divExt;
}

/**********************************************************/
/****** EVENT FUNCTIONS					   ****************/
/**********************************************************/

rialto.widget.AbstractComponent.prototype.onclick=function(e){
// applicative code
};

rialto.widget.AbstractComponent.prototype.ondbleclick=function(e){
// applicative code
};

rialto.widget.AbstractComponent.prototype.onmouseover=function(e){
// applicative code
};

rialto.widget.AbstractComponent.prototype.onmouseout=function(e){
// applicative code
};

rialto.widget.AbstractComponent.prototype.onblur=function(e){
// applicative code
};

rialto.widget.AbstractComponent.prototype.onfocus=function(e){
// applicative code
};

rialto.widget.AbstractComponent.prototype.onmousemove=function(e){
// applicative code
};

rialto.widget.AbstractComponent.prototype.onmousedown=function(e){
//applicative code
};

rialto.widget.AbstractComponent.prototype.onmouseup=function(e){
//applicative code
};
/*
//To add event handler on the external div
rialto.widget.AbstractComponent.prototype.addEvtHandler = function (oHtml){
//=======================================
// to revise : it seems (to me (fl)) useless 
    var oThis=this;
    var oHtml=this.getHtmlExt(); 
    
    oHtml.onclick=function(e){
        var ev=e?e:window.event;
        oThis.onclick(ev);   
    }
        
    oHtml.ondbleclick=function(e){
        var ev=e?e:window.event;
        oThis.ondbleclick(ev);    
    };
    
    oHtml.onmouseover=function(e){
        var ev=e?e:window.event;
        oThis.onmouseover(ev);    
    };
    
    oHtml.onmouseout=function(e){
        var ev=e?e:window.event;
        oThis.onmouseout(ev);    
    };
    
    oHtml.onblur=function(e){
        var ev=e?e:window.event;
        oThis.onblur(ev);    
    };
    
    oHtml.onfocus=function(e){
        var ev=e?e:window.event;
        oThis.onfocus(ev);    
    };
    
    oHtml.onmousemove=function(e){
        var ev=e?e:window.event;
        oThis.onmousemove(ev);    
    };
    
    oHtml.onmousedown=function(e){
        var ev=e?e:window.event;
        oThis.onmousedown(ev);    
    };
    
    oHtml.onmouseup=function(e){
        var ev=e?e:window.event;
        oThis.onmouseup(ev);    
    };
}
*/
/*********************FOR AUTORESIZE *********************/

rialto.widget.AbstractComponent.prototype.getNewParentHeight = function(){
    var obj=this.getHtmlExt();     
    var heightCalc = ria.utils.measures.$getHeight(obj.parentNode,true);    
    return heightCalc;
}


rialto.widget.AbstractComponent.prototype.getNewParentWidth = function(){    
    var obj=this.getHtmlExt();     
    var widthCalc = ria.utils.measures.$getWidth(obj.parentNode,true); 
    return widthCalc;
}

